<template>
  <view @touchmove.stop.prevent="()=>{}" class="container">
    <button class="select-btn" @click="show = true">添加音乐</button>
    <nk-select-file v-model="show" @confirm="getPath"></nk-select-file>
    <scroll-view class="file-list" scroll-y>
      <view class="file-item" v-for="(item, index) in pathArr" :key="index">
        <view class="file-info">
          <text class="file-name">音乐名：{{ item.name }}</text>
        </view>
        <view class="action-buttons">
          <button class="play-btn" @click="selectMusic(item)">选择播放</button>
          <button class="delete-btn" @click="deleteFile(index)">删除</button>
        </view>
      </view>
    </scroll-view>
    <view class="music-disc" :class="{ rotating: isPlaying }" :style="{ transform: `rotate(${rotationAngle}deg)` }">
      <div class="wave-circle"></div>
    </view>
    <view class="current-music">
      <text>当前播放音乐：{{ currentMusic }}</text>
    </view>
    <view class="control-buttons">
      <button class="control-btn" @click="play">▶️️</button>
      <button class="control-btn" @click="pause">⏸️</button>
      <button class="control-btn" @click="stop">⏹️</button>
    </view>
    <view class="progress">
      <text class="current-time">时间：{{ currentTime }} 秒</text>
      <text class="total-time">/{{ duration }} 秒</text>
      <view class="progress-bar">
        <view class="progress-fill" :style="progressStyle"></view>
      </view>
    </view>
	<button @click="goTest('1')">去测试</button>
	<myTabBar class="myTabBar"></myTabBar>
  </view>
</template>

<script>
export default {
  data() {
    return {
      show: false,
      pathArr: [],
      audioContext: null,
      currentTime: 0,
      duration: 0,
      progress: 0,
      isPlaying: false,
      currentMusic: '',
      rotationAngle: 0,
      rotationInterval: null,
    };
  },
  computed: {
    progressStyle() {
      return {
        width: `${(this.progress / 100) * 100}%`,
        background: 'linear-gradient(to right, #00ff00, #ff0000)',
        height: '5px',
      };
    },
  },
  methods: {
    getPath(event) {
      if (event && event.length > 0) {
        event.forEach(file => {
          if (this.isValidMusicFile(file.url)) {
            if (!this.pathArr.some(item => item.url === file.url)) {
              const fileInfo = {
                name: file.name,
                url: file.url,
                sizeMB: (file.size / 1024 / 1024).toFixed(2),
              };
              this.pathArr.push(fileInfo);
            } else {
              uni.showToast({
                title: `文件 ${file.name} 已经添加，无法重复添加`,
                icon: 'none',
                duration: 2000
              });
            }
          } else {
            uni.showToast({
              title: `文件 ${file.name} 被移除，格式不正确`,
              icon: 'none',
              duration: 2000
            });
          }
        });
        this.saveToStorage();
      }
    },
    isValidMusicFile(url) {
      const validExtensions = ['.mp3', '.wav', '.flac', '.aac'];
      const fileExtension = url.slice(url.lastIndexOf('.')).toLowerCase();
      return validExtensions.includes(fileExtension);
    },
    deleteFile(index) {
      this.pathArr.splice(index, 1);
      this.saveToStorage();
    },
    saveToStorage() {
      uni.setStorageSync('musicFiles', this.pathArr);
    },
    loadFromStorage() {
      const storedFiles = uni.getStorageSync('musicFiles');
      if (storedFiles) {
        this.pathArr = storedFiles;
      }
    },
    selectMusic(item) {
      this.currentMusic = item.name;
      this.audioContext = uni.createInnerAudioContext();
      this.audioContext.src = item.url;
      this.audioContext.autoplay = true;

      this.audioContext.onPlay(() => {
        this.isPlaying = true;
        this.duration = Math.floor(this.audioContext.duration);
        this.startRotation();
      });

      this.audioContext.onTimeUpdate(() => {
        this.currentTime = Math.floor(this.audioContext.currentTime);
        this.progress = (this.currentTime / this.duration) * 100;
      });

      this.audioContext.onEnded(() => {
        this.stop();
      });
    },
    startRotation() {
      this.rotationInterval = setInterval(() => {
        this.rotationAngle += 2;
      }, 100);
    },
    stopRotation() {
      clearInterval(this.rotationInterval);
      this.rotationInterval = null;
    },
    play() {
      if (!this.isPlaying && this.audioContext) {
        this.audioContext.play();
      }
    },
    pause() {
      if (this.audioContext && this.isPlaying) {
        this.audioContext.pause();
        this.isPlaying = false;
        this.stopRotation();
      }
    },
    stop() {
      if (this.audioContext) {
        this.audioContext.stop();
        this.isPlaying = false;
        this.currentTime = 0;
        this.progress = 0;
        this.currentMusic = '';
        this.duration = 0;
        this.stopRotation();
      }
    },
	goTest(postId) {
	    uni.navigateTo({
	        url: `/pages/testSend/testSend?postId=${postId}`
	    });
	}
  },
  onLoad() {
    this.loadFromStorage();
  },
};
</script>

<style scoped>
.container {
  margin-top: var(--status-bar-height);
  padding-top: var(--status-bar-height);
  background-color: #f0f0f5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  min-height: 100vh;
  height: 100vh; /* 使用视口高度 */
}
.myTabBar {
  height: 130rpx; /* 设置固定高度 */
}
.select-btn {
  background-color: #007BFF;
  color: white;
  padding: 20rpx 100rpx;
  border: none;
  border-radius: 25rpx;
  font-size: 32rpx;
  margin-bottom: 20rpx;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0, 123, 255, 0.3);
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
}

.select-btn:hover {
  background-color: #0056b3;
  box-shadow: 0 6px 15px rgba(0, 86, 179, 0.4);
}

.file-list {
  width: 100%;
  max-height: 300px;
  overflow-y: auto;
  margin-bottom: 20rpx;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  background-color: #f2f2f2;
}

.file-item {
  background-color: #ffffff;
  padding: 20rpx;
  margin-bottom: 20rpx;
  margin-left: 10rpx;
  margin-right: 10rpx;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: transform 0.2s ease-in-out, box-shadow 0.3s ease;
}

.file-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
}

.file-info {
  margin-bottom: 10rpx;
  color: #333;
}

.file-name {
  font-weight: bold;
  font-size: 28rpx;
  color: #555;
}

.action-buttons {
  display: flex;
  justify-content: space-between;
}

.play-btn, .delete-btn {
  background-color: #28a745;
  color: white;
  font-size: 26rpx;
  padding: 10rpx 20rpx;
  border: none;
  border-radius: 8rpx;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.2s ease;
}

.delete-btn {
  background-color: #dc3545;
}

.play-btn:hover, .delete-btn:hover {
  transform: scale(1.05);
}
.progress-bar {
  width: 80%;
  background-color: #e0e0e0;
  height: 8rpx;
  margin: 10rpx auto;
  border-radius: 10rpx;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  border-radius: 10rpx;
  background-color: #00c853;
  transition: width 0.2s ease-in-out;
}

.current-music {
  margin-top: 30px;
  font-size: 32rpx;
  color: #333;
  font-weight: bold;
  text-align: center;
}

.control-buttons {
  display: flex;
  justify-content: space-between;
  width: 60%;
  margin-top: 30rpx;
}

.control-btn {
  color: white;
  border: none;
  border-radius: 8rpx;
  cursor: pointer;
  font-size: 32rpx;
  transition: background-color 0.3s ease, transform 0.2s ease;
}

.control-btn:hover {
  background-color: aquamarine;
  transform: scale(1.25);
}

.progress {
  width: 100%;
  margin-top: 30px;
  text-align: center;
}

.music-disc {
  width: 200rpx;
  height: 200rpx;
  border-radius: 50%;
  background: conic-gradient(#ccc 0%, #bbb 20%, #ccc 20%, #ccc 80%, #bbb 80%, #ccc 100%);
  position: relative;
  margin-top: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.wave-circle {
  position: absolute;
  width: 220rpx;
  height: 220rpx;
  border-radius: 50%;
  border: 5px solid transparent;
  background: radial-gradient(circle at center, transparent 60%, rgba(0, 128, 255, 0.5) 70%);
  animation: wave 1.5s infinite linear;
}

@keyframes wave {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}
</style>
